package cn.mesmile.groovy.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.mesmile.groovy.core.GroovyDynamicLoader;
import cn.mesmile.groovy.dto.GroovyRuleInfoAddDTO;
import cn.mesmile.groovy.dto.GroovyRuleInfoUpdateDTO;
import cn.mesmile.groovy.entity.GroovyRuleInfo;
import cn.mesmile.groovy.mapper.GroovyRuleInfoMapper;
import cn.mesmile.groovy.service.GroovyRuleInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Objects;

/**
 * @author zb
 * @date 2021/11/25 15:01
 * @Description
 */
@Transactional(rollbackFor = Exception.class,propagation= Propagation.SUPPORTS,readOnly = true)
@Service
public class GroovyRuleInfoServiceImpl extends ServiceImpl<GroovyRuleInfoMapper, GroovyRuleInfo> implements GroovyRuleInfoService {

	private GroovyDynamicLoader getGroovyDynamicLoader(){
		return SpringUtil.getBean(GroovyDynamicLoader.class);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean addGroovyRuleInfo(GroovyRuleInfoAddDTO groovyRuleInfoAddDTO) {
		String interfaceId = groovyRuleInfoAddDTO.getInterfaceId();
		String beanName = groovyRuleInfoAddDTO.getBeanName();
		Assert.isTrue(StrUtil.isNotBlank(interfaceId), "接口id不允许为空");
		Assert.isTrue(StrUtil.isNotBlank(beanName), "beanName不允许为空");
		// 检查 接口id唯一/beanName唯一
		addGroovyRuleInfoCheck(interfaceId, beanName);

		GroovyRuleInfo groovyRuleInfo = new GroovyRuleInfo();
		BeanUtils.copyProperties(groovyRuleInfoAddDTO, groovyRuleInfo);
		DateTime dateTime = new DateTime();
		groovyRuleInfo.setCreateTime(dateTime);
		groovyRuleInfo.setUpdateTime(dateTime);
		if (groovyRuleInfoAddDTO.getStatus() == null){
			groovyRuleInfo.setStatus(true);
		}
		boolean result = save(groovyRuleInfo);
		if (result){
			// 刷新groovy规则
			getGroovyDynamicLoader().refresh();
		}
		return result;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean updateGroovyRuleInfoById(GroovyRuleInfoUpdateDTO groovyRuleInfoUpdateDTO) {
		String beanName = groovyRuleInfoUpdateDTO.getBeanName();
		String interfaceId = groovyRuleInfoUpdateDTO.getInterfaceId();
		Long id = groovyRuleInfoUpdateDTO.getId();
		Assert.isTrue(StrUtil.isNotBlank(interfaceId), "接口id不允许为空");
		Assert.isTrue(StrUtil.isNotBlank(beanName), "beanName不允许为空");
		// 检查接口id 和 beanName
		updateGroovyRuleCheck(beanName, interfaceId, id);
		// 拷贝
		GroovyRuleInfo groovyRuleInfo = new GroovyRuleInfo();
		BeanUtils.copyProperties(groovyRuleInfoUpdateDTO, groovyRuleInfo);
		if (groovyRuleInfo.getStatus() == null){
			// 默认可用
			groovyRuleInfo.setStatus(true);
		}
		boolean result = updateById(groovyRuleInfo);
		if (result){
			// 刷新groovy规则
			getGroovyDynamicLoader().refresh();
		}
		return result;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean deleteRuleInfoById(Long id) {
		boolean result = removeById(id);
		if (result){
			// 刷新groovy规则
			getGroovyDynamicLoader().refresh();
		}
		return result;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean deleteRuleInfoByInterfaceId(String interfaceId) {
		LambdaQueryWrapper<GroovyRuleInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(GroovyRuleInfo::getInterfaceId, interfaceId);
		boolean result = remove(lambdaQueryWrapper);
		if (result){
			// 刷新groovy规则
			getGroovyDynamicLoader().refresh();
		}
		return result;
	}


	private void addGroovyRuleInfoCheck(String interfaceId, String beanName) {
		// 判断 接口id唯一
		LambdaQueryWrapper<GroovyRuleInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(GroovyRuleInfo::getInterfaceId,interfaceId);
		int count = count(lambdaQueryWrapper);
		Assert.isTrue(count == 0,"已经存在接口id为："+ interfaceId +" 的规则");
		// 判断 beanName唯一
		LambdaQueryWrapper<GroovyRuleInfo> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
		lambdaQueryWrapper2.eq(GroovyRuleInfo::getBeanName,beanName);
		int count1 = count(lambdaQueryWrapper2);
		Assert.isTrue(count1 == 0,"已经存在bean名称为："+ beanName +" 的规则");
	}

	private void updateGroovyRuleCheck(String beanName, String interfaceId, Long id) {
		GroovyRuleInfo oldGroovyRuleInfo = getById(id);
		Assert.isTrue(oldGroovyRuleInfo != null, "当前规则不存在");
		String oldBeanName = oldGroovyRuleInfo.getBeanName();
		String oldInterfaceId = oldGroovyRuleInfo.getInterfaceId();
		if (!Objects.equals(beanName, oldBeanName)){
			// 判断 beanName唯一
			LambdaQueryWrapper<GroovyRuleInfo> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
			lambdaQueryWrapper2.eq(GroovyRuleInfo::getBeanName,beanName);
			int count1 = count(lambdaQueryWrapper2);
			Assert.isTrue(count1 == 0,"已经存在bean名称为："+ beanName +" 的规则");
		}
		if (!Objects.equals(interfaceId, oldInterfaceId)){
			// 判断 接口id唯一
			LambdaQueryWrapper<GroovyRuleInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
			lambdaQueryWrapper.eq(GroovyRuleInfo::getInterfaceId,interfaceId);
			int count = count(lambdaQueryWrapper);
			Assert.isTrue(count == 0,"已经存在接口id为："+ interfaceId +" 的规则");
		}
	}


}
